package com.zhxy.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.EquipmentDao;
import com.zhxy.manager.entity.Equipment;
import com.zhxy.manager.excel.EquipmentExcel;
import com.zhxy.manager.service.intf.EquipmentService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentDao dao;
    //增加
    @Override
    public R save(Equipment equipment) {
        if (dao.add(equipment)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }
    //查询全部
    @Override
    public List<Equipment> queryAll() {
        return dao.selectAll();
    }
    //删除
    @Override
    public R del(int id) {
        if (id>0){
            if (dao.del(id)>0){
                return R.ok();
            }else{
                return R.fail();
            }
        }else{
            return R.fail();
        }
    }
    //修改
    @Override
    public R update(Equipment equipment) {
        if (dao.update(equipment)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }
    //条件查询
    @Override
    public PageBean queryPageWhere(String name, int page, int limit) {
        //1.验证 查询条件 如果为 "" 那么赋值为null
        if(!StringUtils.hasLength(name)){
            name=null;
        }else{
            name="%"+name+"%";//包含  模糊查询
        }
        PageHelper.startPage(page,limit);
        PageInfo<Equipment> pageInfo = new PageInfo<>(dao.selectWhere(name));
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }
    //批量查询
    @Override
    public R delBatch(int[] ids) {
        if (ids != null & ids.length > 0) {
            if (dao.delBatch(ids) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        }else{
            return R.fail();
        }
    }
    //批量增加
    @Override
    public R saveBatch(MultipartFile file) throws IOException {
        if (!file.isEmpty()){
            List<EquipmentExcel> list = EasyExcel.read(file.getInputStream(), EquipmentExcel.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {}
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
            }).sheet().doReadSync();
            if (dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

}
